<template>
  <div class="expricing result">
    <div class="expricingForm" style="border:solid 1px #ccc;">
      <el-form
        :model="searchForm"
        ref="searchRef"
        label-width="150px"
        style="margin-top:10px"
        label-position="right"
      >
        <el-row>
          <el-col :span="6">
            <el-form-item label="头寸数据表：" prop="positions">
              <el-input v-model="positions" readonly></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="数据日期：" prop="dataDate">
              <el-date-picker
                v-model="searchForm.dataDate"
                type="date"
                style="width:100%"
                value-format="yyyy-MM-dd"
                placeholder
              ></el-date-picker>
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="定价结果：" prop="status">
              <el-select v-model="searchForm.status" style="width:100%" placeholder="请选择">
                <el-option
                  v-for="item in statusOption"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="账号：" prop="accountNum">
              <el-input v-model="searchForm.accountNum" maxlength="32"></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="6">
            <el-form-item label="机构：" prop="orgCode">
              <div class="treeBoxBig">
                <div class="treeBox">{{ searchForm.org }}</div>
                <div class="treeBtnCS" @click="treeBtnorg"></div>
              </div>
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="科目: " prop="subjectCode">
              <div class="treeBoxBig">
                <div class="treeBox">{{ searchForm.subject }}</div>
                <div class="treeBtnCS" @click="treeBtnsubject"></div>
              </div>
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="币种：" prop="currencyCode">
              <el-select v-model="searchForm.currencyCode" style="width:100%" placeholder="请选择">
                <el-option
                  v-for="item in currencyOption"
                  :key="item.currencyId"
                  :label="item.currencyName"
                  :value="item.currencyId"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="产品：" prop="productCode">
              <div class="treeBoxBig">
                <div class="treeBox">{{ searchForm.product }}</div>
                <div class="treeBtnCS" @click="treeBtnproduct"></div>
              </div>
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="6">
            <el-form-item label="行业：" prop="industryCode">
              <div class="treeBoxBig">
                <div class="treeBox">{{ searchForm.industry }}</div>
                <div class="treeBtnCS" @click="treeBtnindustry"></div>
              </div>
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="起息日：" prop="orgDate">
              <el-date-picker
                v-model="searchForm.orgDate"
                type="date"
                style="width:100%"
                format="yyyy-MM-dd"
                value-format="yyyy-MM-dd"
                placeholder
              ></el-date-picker>
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="原始期限：" prop="orgTerm">
              <!-- <el-form-item label="指定期限：" prop="assignTerm"> -->
              <el-col :span="10">
                <el-input v-model.trim="searchForm.orgTermL" maxlength="40"></el-input>
              </el-col>
              <el-col :span="14">
                <el-select v-model="searchForm.orgTermdate" placeholder="请选择" style="width:100%">
                  <el-option
                    v-for="item in dateOption"
                    :key="item.value"
                    :label="item.label"
                    :value="item.value"
                  ></el-option>
                </el-select>
              </el-col>
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="利率类型：" prop="rateTypeCd">
              <el-select v-model="searchForm.rateTypeCd" style="width:100%" placeholder="请选择">
                <el-option
                  v-for="item in rateTypeCdOption"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="6">
            <el-form-item label="支付类型：" prop="payTypeCd">
              <el-select v-model="searchForm.payTypeCd" style="width:100%" placeholder="请选择">
                <el-option
                  v-for="item in payTypeCdOption"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="重定价频率：" prop="repriceFreq">
              <el-col :span="10">
                <el-input v-model.trim="searchForm.repriceFreqL" maxlength="40"></el-input>
              </el-col>
              <el-col :span="14">
                <el-select v-model="searchForm.rpricFreqMult" placeholder="请选择" style="width:100%">
                  <el-option
                    v-for="item in dateOption"
                    :key="item.value"
                    :label="item.label"
                    :value="item.value"
                  ></el-option>
                </el-select>
              </el-col>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row class="container" style="text-align:center">
          <el-col style="margin-bottom:10px;" class="content-left">
            <el-button @click="searchFunc()">查询</el-button>
            <el-button @click="reset()">重置</el-button>
          </el-col>
        </el-row>
      </el-form>
    </div>

    <div class="expricingTable">
      <el-row class="container">
        <el-col style="margin:10px 0px 10px 0px" class="content-left">
          <el-button @click="exportExPricingResult()">导出</el-button>
          <el-button @click="showCashFlow()">查看现金流</el-button>
          <el-button @click="toForm()">查看现详情</el-button>
        </el-col>
      </el-row>
      <el-table
        ref="multipleModelTable"
        :data="tableModelData"
        style="width: 100%"
        @row-click="authorizeModelFor"
      >
        <el-table-column width="55" type="selection" ></el-table-column>
        <el-table-column prop="acctNo" label="账号" :show-overflow-tooltip="true"></el-table-column>
        <el-table-column prop="dataDt" label="数据日期" :show-overflow-tooltip="true"></el-table-column>
        <el-table-column prop="orgCdDisp" label="机构" :show-overflow-tooltip="true"></el-table-column>
        <el-table-column prop="prdCdDisp" label="产品" :show-overflow-tooltip="true"></el-table-column>
        <el-table-column prop="sbjtCdDisp" label="科目" :show-overflow-tooltip="true"></el-table-column>
        <el-table-column prop="ccyCdDisp" label="币种" :show-overflow-tooltip="true"></el-table-column>
        <el-table-column prop="curBal" label="当前余额" :show-overflow-tooltip="true"></el-table-column>
        <el-table-column prop="startDt" label="起息日" :show-overflow-tooltip="true"></el-table-column>
        <el-table-column prop="execRate" label="对客利率" :show-overflow-tooltip="true"></el-table-column>
        <el-table-column prop="transRate" label="基础转移价格" :show-overflow-tooltip="true"></el-table-column>
        <el-table-column prop="finalTransRate" label="最终转移价格" :show-overflow-tooltip="true"></el-table-column>
        <el-table-column prop="compstPricAdjst" label="组合定价调整项" :show-overflow-tooltip="true"></el-table-column>
        <el-table-column prop="transIe" label="转移收支" :show-overflow-tooltip="true"></el-table-column>
        <el-table-column prop="bcRuleRes" label="BreakCharge结果" :show-overflow-tooltip="true"></el-table-column>
        <el-table-column prop="bfRuleRes" label="BreakFunding结果" :show-overflow-tooltip="true"></el-table-column>
        <el-table-column prop="accumGalY" label="年度累计损益" :show-overflow-tooltip="true"></el-table-column>
        <el-table-column prop="accumGal" label="累计损益" :show-overflow-tooltip="true"></el-table-column>
        <el-table-column prop="hisBalAccum" label="历史余额累计" :show-overflow-tooltip="true"></el-table-column>
        <el-table-column prop="startExecRate" label="起息日的执行利率" :show-overflow-tooltip="true"></el-table-column>
        <el-table-column prop="startBaseFtp" label="起息日的基础转移价格" :show-overflow-tooltip="true"></el-table-column>
        <el-table-column prop="startFinaFtp" label="起息日的最终转移价格" :show-overflow-tooltip="true"></el-table-column>
        <el-table-column prop="lastRpricExecRate" label="上次重定价日执行利率" :show-overflow-tooltip="true"></el-table-column>
        <el-table-column prop="lastRpricBaseFtp" label="上次重定价日基础价格" :show-overflow-tooltip="true"></el-table-column>
        <el-table-column prop="lastRpricFinaFtp" label="上次重定价日最终价格" :show-overflow-tooltip="true"></el-table-column>
        <el-table-column prop="rpricFinaFtp" label="上次重定价余额" :show-overflow-tooltip="true"></el-table-column>
        <el-table-column prop="origAmt" label="原始余额" :show-overflow-tooltip="true"></el-table-column>
        <el-table-column prop="matuDt" label="到期日" :show-overflow-tooltip="true"></el-table-column>
        <el-table-column prop="term" label="原始期限" :show-overflow-tooltip="true"></el-table-column>
        <el-table-column prop="rpricDtAdjstTp" label="重定价类型代码" :show-overflow-tooltip="true"></el-table-column>
        <el-table-column prop="lastRepricDt" label="上次重定价日期" :show-overflow-tooltip="true"></el-table-column>
        <el-table-column prop="nextRepricDt" label="下次重定价日期" :show-overflow-tooltip="true"></el-table-column>
        <el-table-column prop="repricFreq" label="重定价频率" :show-overflow-tooltip="true"></el-table-column>
        <el-table-column prop="pmtTpDisp" label="支付类型" :show-overflow-tooltip="true"></el-table-column>
        <el-table-column prop="lastPrinPmtDt" label="上次支付日" :show-overflow-tooltip="true"></el-table-column>
        <el-table-column prop="nextPrinPmtDt" label="下次支付日" :show-overflow-tooltip="true"></el-table-column>
      </el-table>
      <!-- 分页插件 -->
      <pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="searchForm.page"
        :page-size="searchForm.size"
        @refresh="searchFunc"
        :total="total"
      ></pagination>
    </div>
    <!-- 树选择框的弹框 -->
    <el-dialog
      v-dialogDrag
      title="选择值"
      :visible.sync="showMemberTree"
      append-to-body
      width="55%"
      top="2%"
      @close="cancel"
    >
      <!-- 引入 -->

      <relation-tree
        ref="relationTree"
        :indexItems="indexItems"
        :itemData="itemData"
        :dictCode="dictCode"
        @selectDataFunc="selectDataFunc"
      ></relation-tree>
      <!-- :mulSelect="mulSelect" -->
    </el-dialog>
    <!-- 树选择框的弹框 结束-->

    <!-- 查看现金流 -->
    <el-dialog title="查看现金流" v-dialogDrag width="70%" top="2%" :visible.sync="showCashFlowOper">
      <cash-flow :param="cashFlowParam" :flag="showCashFlowOper"></cash-flow>
      <div slot="footer">
        <el-button @click="showCashFlowOper = false">关 闭</el-button>
      </div>
    </el-dialog>

    <!-- 查看现金流结束 -->

    <!-- 查看详情页面 -->
    <el-dialog
      v-dialogDrag
      title="查看详情"
      :visible.sync="resultFormDi"
      append-to-body
      width="70%"
      top="2%"
    >
      <!-- 引入 -->

      <resultForm ref="resultFormRef" :exResultParam="exResultParam"></resultForm>
      <!-- :mulSelect="mulSelect" -->
      <div slot="footer">
        <!-- <el-button @click="saveFunc">确 定</el-button> -->
        <el-button @click="resultFormDi = false">返 回</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
import { pricingTask, expricing } from "@/api/index";
import { getDict } from "@/api/getDict";
import { publicManage_pub } from "@/api/index";
import relationTree from "@/components/params/relationTree";
//现金流
import cashFlow from "./cashFlow";
//查看详情
import resultForm from "./exTaskResult_form";

import upperTemplate from "@/components/upperTemplate.vue";
import ruleDefComponents from "@/components/ca/ruleDefComponents";

export default {
  data() {
    return {
      //异常定价编码暂时选定
      pricingCode: "DJ20210330006",
      //任务名称
      taskName: "异常定价",
      //头寸数据表
      positions: "FTP_PD_RESULT_EXCEPTION",
      //查看详情
      exResultParam: {
        acctNo: "",
        dataDt: "",
        positionTableFix: ""
      },
      resultFormDi: false,
      product: "",
      org: "",
      industry: "",
      subject: "",
      //选择值组件所需信息
      showMemberTree: false,
      dictCode: "OrganUnit",
      indexItems: 0,
      itemData: [],
      resetSearchFrom: {
        page: 1,
        size: 15
      },
      searchForm: {
        page: 1,
        size: 15
      },
      //币种
      currencyOption: [],
      //年月日
      dateOption: [],
      //头寸数据表
      positionOptions: [],
      //定价状态
      statusOption: [],
      //支付类型
      payTypeCdOption: [],
      //利率类型
      rateTypeCdOption: [],
      tableModelData: [],
      total: null,
      //查看现金流
      showCashFlowOper: false,
      cashFlowParam: {}
    };
  },
  watch: {},
  components: {
    upperTemplate,
    ruleDefComponents,
    relationTree,
    resultForm,
    cashFlow
  },
  created() {
    this.searchFunc();
    this.getDict("ftp_pricing_status");
    this.getDict("ftp_cp_rate_type_cd");
    this.getDict("ftp_cp_pay_type_cd");
    this.getDict("ftp_cp_time_type");
    this.getDict("ftp_pricing_task_status");
    // this.findPositionTableList();
    this.queryDimCurrency();
    this.findPositionTableList();
  },
  computed: {},
  mounted() {},
  methods: {
    //异常定价初始化方法
    toForm() {
      let selection = this.$refs.multipleModelTable.selection;
      if (selection.length != 1) {
        return this.$message.warning("请选择一条数据");
      }
      this.exResultParam = {
        acctNo: selection[0].acctNo,
        dataDt: selection[0].dataDt,
        positionTableFix: selection[0].pdPostFix
      };
      console.log(this.exResultParam, "this.exResultParam");
      //调用结果详情插件中search 方法
      this.resultFormDi = true;
      this.$nextTick(() => {
        this.$refs.resultFormRef.search();
      });
    },
    reset() {
      //深拷贝数据
      this.searchForm = this.$clone(this.resetSearchFrom);
    },
    searchFunc() {
      //首先检验是否有权限查看数据
      expricing.checkCurrentOrganPrivilege().then(result => {
        //有权限查看数据
        if (result.code == 200) {
          console.log(this.searchForm, "怎么根绝他不调用这个方法？ ");
          let data = {
            page: this.searchForm.page,
            size: this.searchForm.size,
            acctNo: this.searchForm.accountNum,
            ccyCd: this.searchForm.currencyCode,
            dataDt: this.searchForm.dataDate,
            industryCd: this.searchForm.industryCd,
            orgCd: this.searchForm.industryCd,
            startDt: this.searchForm.orgCd,
            term: this.searchForm.orgTermL,
            termMult: this.searchForm.orgTermdate,
            pmtTp: this.searchForm.payTypeCd,
            positionTableFix: this.searchForm.pdName,
            pricStat: this.searchForm.status,
            prdCd: this.searchForm.prdCd,
            rateTp: this.searchForm.rateTypeCd,
            rpricFreq: this.searchForm.repriceFreqL,
            rpricFreqMult: this.searchForm.rpricFreqMult,
            sbjtCd: this.searchForm.sbjtCd
          };
          console.log(data, "传递的数据");
          expricing.findAllExResult(data).then(res => {
            if (res.code == 200) {
              console.log(res, "findall查出来的数===========");
              this.tableModelData = res.data.rows;
            } else {
              this.$message.error("查询失败");
            }
          });
        }else{
            this.$message.warning(result.message)
        }
      });
    },
    queryDimCurrency() {
      let data = {
        currencyCode: "",
        currencyDescribe: "",
        currencyFlag: "",
        currencyName: "",
        currencySign: "",
        order: "",
        orderBy: "",
        page: 0,
        size: 0,
        status: ""
      };
      console.log(data, "data");
      publicManage_pub.currencyManageQuery(data).then(res => {
        if (res.code == 200) {
          this.currencyOption = res.data;
          console.log(res, "chadaodeshujv===========");
        } else {
          this.$message.error(res.message);
        }
      });
    },
    findPositionTableList() {
      pricingTask.findPositionTableList().then(res => {
        if (res.code == 200) {
          this.positionOptions = res.datas;
        } else {
          this.$message.error(res.message);
        }
      });
    },
    getDict(data) {
      getDict(data).then(res => {
        if (res.code == 200) {
          if (data == "ftp_pricing_status") {
            this.statusOption = res.datas;
          } else if (data == "ftp_cp_rate_type_cd") {
            this.rateTypeCdOption = res.datas;
          } else if (data == "ftp_cp_pay_type_cd") {
            this.payTypeCdOption = res.datas;
          } else if (data == "ftp_cp_time_type") {
            this.dateOption = res.datas;
          } else if (data == "ftp_pricing_task_status") {
            this.statusOption = res.datas;
          }
        } else {
          this.$message.error(res.message);
        }
      });
    },
    treeBtnorg() {
      this.dictCode = "OrganUnit";
      this.treeBtn();
    },
    treeBtnsubject() {
      this.dictCode = "GeneralLedger";
      this.treeBtn();
    },
    treeBtnproduct() {
      this.dictCode = "Product";
      this.treeBtn();
    },
    treeBtnindustry() {
      this.dictCode = "Industry";
      this.treeBtn();
    },
    cancel() {
      this.showMemberTree = false;
    },
    selectDataFunc(val) {
      this.showMemberTree = false;
      if (val) {
        let params = val;
        console.log(params, "params");
        console.log(params.row, "params.row[0]");

        if (this.dictCode == "GeneralLedger") {
          this.searchForm.subject = params.row.dictItemName;
          this.searchForm.sbjtCd = params.row.dictItemCode;
        } else if (this.dictCode == "OrganUnit") {
          this.searchForm.org = params.row.dictItemName;
          this.searchForm.orgCd = params.row.dictItemCode;
        } else if (this.dictCode == "Product") {
          this.searchForm.product = params.row.dictItemName;
          this.searchForm.prdCd = params.row.dictItemCode;
        } else {
          this.searchForm.industry = params.row.dictItemName;
          this.searchForm.industryCd = params.row.dictItemCode;
        }
      }
    },
    //查看现金流
    showCashFlow() {
      //选中的数据
      let selects = this.$refs.multipleModelTable.selection;
      if (selects.length != 1) {
        return this.$message.warning("请选择一条数据查看现金流");
      }
      let row = selects[0];
      this.cashFlowParam = {
        dataDt: row.dataDt,
        acctNo: row.acctNo,
        tableName: row.pdPostFix
      };
      this.showCashFlowOper = true;
    },
    treeBtn() {
      this.showMemberTree = true;
      
    },
    //点击行选中
    authorizeModelFor(row) {
      if (this.$refs.multipleModelTable) {
        this.$refs.multipleModelTable.clearSelection();
      }
      this.$refs.multipleModelTable.toggleRowSelection(row, true);
    },
    handleSizeChange(val) {
      this.searchForm.size = val;
      this.searchFunc();
      console.log(val, "每页数");
    },
    handleCurrentChange(val) {
      this.searchForm.page = val;
      this.searchFunc();
      console.log(val, "当前页");
    },
    //导出异常结果数据
    exportExPricingResult() {
      //判断是根据条件查询  还是根据选择查询
      let selects = this.$refs.multipleModelTable.selection;
      let data = {};
      if (selects.length == 0) {
        data = {
          acctNo: this.searchForm.accountNum,
          ccyCd: this.searchForm.currencyCode,
          dataDt: this.searchForm.dataDate,
          industryCd: this.searchForm.industryCd,
          orgCd: this.searchForm.industryCd,
          startDt: this.searchForm.orgCd,
          term: this.searchForm.orgTermL,
          termMult: this.searchForm.orgTermdate,
          pmtTp: this.searchForm.payTypeCd,
          positionTableFix: this.searchForm.pdName,
          pricStat: this.searchForm.status,
          prdCd: this.searchForm.prdCd,
          rateTp: this.searchForm.rateTypeCd,
          rpricFreq: this.searchForm.repriceFreqL,
          rpricFreqMult: this.searchForm.rpricFreqMult,
          sbjtCd: this.searchForm.sbjtCd
        };
      } else {
        let selectDatas = [];
        for (let i = 0; i < selects.length; i++) {
          selectdatas.push(selects[i].PK);
        }
        data = {
          selectedData: selectDatas
        };
      }
      expricing.expExpricingResult(data).then(response => {
        debugger;
        console.log(response, "response");
        if (response.code == 500) {
          this.$message.error(response.message);
        } else if (response) {
          console.log(decodeURI(response.headers["content-disposition"]));
          var filename = decodeURI(
            response.headers["content-disposition"]
          ).split("=")[1];
          let contenttype =
            filename.substring(filename.lastIndexOf("."), filename.length) ===
            "xlsx"
              ? "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;charset=utf-8"
              : "application/vnd.ms-excel;charset=utf-8";

          var blob = new Blob([response.data], {
            type: contenttype
          });
          if (window.navigator.msSaveOrOpenBlob) {
            navigator.msSaveBlob(blob, filename);
          } else {
            var downloadElement = document.createElement("a");
            var href = window.URL.createObjectURL(blob); //创建下载的链接
            downloadElement.href = href;
            downloadElement.download = filename; //下载后文件名
            document.body.appendChild(downloadElement);
            downloadElement.click(); //点击下载
            document.body.removeChild(downloadElement); //下载完成移除元素
            window.URL.revokeObjectURL(href); //释放掉blob对象
          }
          this.$message.success("导出成功");
        } else {
          this.$message.error(response.message || "获取失败");
        }
      });
    }
  }
};
</script>

<style lang="scss" scoped>
.treeBoxBig {
  // width: 320px;
  width: 100%;
  height: 32px;
  border: 1px solid #ababab;
  border-radius: 4px;
  .treeBox {
    width: 100%;
    height: 32px;
    padding-left: 10px;
    line-height: 32px;
    overflow: hidden;
  }
  .treeBtnCS {
    background: url("~@/assets/images/popWindow_arrow.gif");
    background-repeat: no-repeat;
    height: 32px;
    width: 32px;
    position: absolute;
    // left: 296px;
    right: 0;
    top: 8px;
    cursor: pointer;
  }
}
</style>
<style lang="scss" scoped>
.dialogSearchBox {
  header {
    text-align: center;
    span {
      margin: 0 20px;
    }
  }
}
.selectBox {
  background: url("~@/assets/images/popWindow_arrow.gif");
  background-repeat: no-repeat;
  height: 26px;
  width: 23px;
  position: absolute;
  right: 0;
  top: 5px;
  cursor: pointer;
}
</style>
